import type { App } from 'vue'
import {
  createRouter,
  createWebHashHistory,
  type RouteRecordRaw,
} from 'vue-router'
import { useAuthStore } from '@/store/modules/auth'

export const Layout = () => import('@/layout/index.vue')

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: { hidden: true },
  },
  {
    path: '/markdown',
    component: () => import('@/views/MarkdownEditor.vue'),
    meta: { title: 'Markdown编辑器', icon: 'document' },
  },
  {
    path: '/',
    name: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        name: 'Dashboard',
        meta: {
          title: '首页',
          icon: 'homepage',
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: 'article_detail',
        component: () => import('@/views/article-detail/index.vue'),
        name: 'ArticleDetail',
        meta: {
          title: '文章详情',
        },
      },
      {
        path: 'share',
        component: () => import('@/views/components/SgArticleList/index.vue'),
        name: 'Share',
        meta: {
          title: '分类',
          icon: 'heart',
        },
      },
      {
        path: 'appreciation',
        component: () => import('@/views/Appreciation.vue'),
        name: 'Appreciation',
        meta: {
          title: '赞赏支持',
          icon: 'heart',
        },
      },
      {
        path: 'friend_links',
        component: () => import('@/views/FriendLinks.vue'),
        name: 'FriendLinks',
        meta: {
          title: '友情链接',
          icon: 'link',
        },
      },
      {
        path: 'chat',
        component: () => import('@/views/chat/index.vue'),
        name: 'Chat',
        meta: {
          title: 'DeepSeek',
          icon: '',
        },
      },
      {
        path: 'user_info',
        component: () => import('@/views/UserInfo.vue'),
        name: 'UserInfo',
        meta: {
          title: '个人中心',
          icon: 'link',
        },
      },
      {
        path: '401',
        component: () => import('@/views/error/401.vue'),
        meta: { hidden: true },
      },
      {
        path: '404',
        component: () => import('@/views/error/404.vue'),
        meta: { hidden: true },
      },
    ],
  },
]

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  // scrollBehavior: () => ({ left: 0, top: 0 }),
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore()
  const isAuthenticated = await authStore.isAuthenticated
  // 如果已登录且访问登录页，重定向到首页
  if (isAuthenticated && to.path === '/login') {
    next('/')
    return
  }

  // 如果未登录且访问非登录页，重定向到登录页
  if (!isAuthenticated && to.path !== '/login') {
    next('/login')
    return
  }

  next()
})

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router)
}

export default router
